import 'package:flutter/material.dart';
import 'package:im_flutter_sdk_example/utils/global.dart';

import 'labels/index.dart';

class Tab extends StatelessWidget {
  final String title;
  final bool active;
  final Function press;

  const Tab({
    Key key,
    @required this.title,
    this.active,
    this.press,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    double width = (G.screenWidth() - 2 * 15) / 6;
    if (active) {
      return GestureDetector(
        onTap: press,
        child: Container(
          width: width,
          padding: EdgeInsets.symmetric(horizontal: 8),
          child: LabelTitle(
            title: '$title',
          ),
        ),
      );
    }
    return GestureDetector(
      onTap: press,
      child: Container(
          width: width,
          child: Text('$title'),
          padding: EdgeInsets.symmetric(horizontal: 8)),
    );
  }
}

class Tabs extends StatefulWidget {
  final Function onChange;
  final int actIndex;

  const Tabs({
    Key key,
    this.onChange,
    this.actIndex,
  }) : super(key: key);

  @override
  _TabsState createState() => _TabsState();
}

class TabModel {
  final String title;
  final int idx;

  TabModel(this.title, this.idx);
}

class _TabsState extends State<Tabs> {
  int activeIndex = 0;
  List<TabModel> tabs = [
    TabModel("推荐", 1),
    TabModel("专家", 2),
    TabModel("项目", 3),
    TabModel("活动", 4),
    TabModel("需求", 5),
    TabModel("服务", 6),
  ];

  changeIndex(int index) {
    setState(() {
      activeIndex = index;
    });
    widget.onChange(index);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      color: Colors.white,
      padding: EdgeInsets.only(top: 8, left: 10, right: 16, bottom: 10),
      child: Row(
          children: tabs
              .map((e) => Tab(
                    title: e.title,
                    active: widget.actIndex == e.idx,
                    press: () => changeIndex(e.idx),
                  ))
              .toList()),
    );
  }
}
